package com.epam.railway.jdbc.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserFilter implements Filter {

	HttpSession session;

	@Override
	public void destroy() {
		//session.invalidate();
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		session = request.getSession();
		String email = request.getParameter("email");
		String pwd = request.getParameter("password");
		String result;
		System.out.println(email);
		System.out.println(pwd);
		if (email.equals("admin@admin") && pwd.equals("admin")) {
			result = "good";
			System.out.println(result);
			request.getRequestDispatcher("/pages/home.jsp").forward(request,
					response);
		} else {
			result = "bad";
			request.setAttribute("error", "username or password error!");
			request.getRequestDispatcher("/pages/login.jsp").forward(request,
					response);
		}

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
